//
//  ZhiShiViewController.swift
//  Community
//
//  Created by MAC on 2019/8/7.
//  Copyright © 2019 ZKJ. All rights reserved.
//

import UIKit

class ZhiShiViewController: UIViewController {

 
    var label = UILabel()
    override func viewDidLoad() {
        super.viewDidLoad()

        fd_prefersNavigationBarHidden = true
       
        
        let zhong = UIImageView()
        zhong.frame = CGRect(x: 0, y: 0, width: kScreenW, height: kScreenH)
        zhong.image = UIImage(named: "buchong")
        zhong.contentMode = .scaleToFill
        self.view.addSubview(zhong)
        
        let header = UIImageView()
        header.frame = CGRect(x: 0, y: NavBarHeight, width: kScreenW, height: constant.AdaptedHeight(x: 207))
        header.image = UIImage(named: "header")
        self.view.addSubview(header)
        
        
        let footer = UIImageView()
        footer.frame = CGRect(x: 0, y: kScreenH - constant.AdaptedHeight(x: 182) - 40 - NavBarHeight, width: kScreenW, height: constant.AdaptedHeight(x: 182))
        footer.image = UIImage(named: "footer")
        self.view.addSubview(footer)
        
        self.view.backgroundColor = mainGreenColor
        
        
        
        label.frame = CGRect(x: 15, y: constant.AdaptedHeight(x: 207), width: kScreenW - 30, height: kScreenH - header.n_maxY - NavBarHeight)
        label.font = UIFont.systemFont(ofSize: 16)
        
        label.textColor = .white
        label.numberOfLines = 0
        
       
        self.view.addSubview(label)
        
        label.snp.makeConstraints { (make) in
            make.left.equalToSuperview().offset(20)
            make.right.equalToSuperview().offset(-20)
            make.top.equalTo(header.snp.bottom).offset(20)
        }
        
        
        let diaryList:String = Bundle.main.path(forResource: "zhishi", ofType:"plist")!
        let data = NSDictionary.init(contentsOfFile: diaryList)
        let dataList = data!["list"] as! [String]
        
        let index = arc4random_uniform(UInt32(dataList.count-1))
        
    
        let attr = NSMutableAttributedString.init(string: dataList[Int(index)])
        attr.lineSpacing = 4
        attr.lineBreakMode = .byTruncatingTail
        label.attributedText = attr
        
        
        let back = UIButton(type: UIButton.ButtonType.custom)
        back.frame = CGRect(x: 15,y: NavBarHeight,width: 44,height: 44)
        back.setImage(UIImage.init(named: "black_return"), for: UIControl.State.normal)
        back.contentHorizontalAlignment = UIControl.ContentHorizontalAlignment.left
        back.addTarget(self, action: #selector(backAct), for: UIControl.Event.touchUpInside)
        self.view.addSubview(back)
        
        let sb = UIButton.init(type: UIButton.ButtonType.custom)
        sb.frame = CGRect (x: kScreenW - 15 - 90, y: NavBarHeight + 7, width: 90, height: 30)
        sb.layer.cornerRadius = 5
        sb.clipsToBounds = true
        sb.backgroundColor = mainGreenColor //UIColor.init(r: 51/255.0, g: 150/255/0, b: 251/255.0, alpha: 1)
        sb.setTitle("随机小知识", for: UIControl.State.normal)
        sb.setTitleColor(UIColor.white, for: UIControl.State.normal)
        sb.titleLabel?.font = constant.kFontSize(x: 14)
        self.view.addSubview(sb)
        sb.addTarget(self, action: #selector(tosearch), for: UIControl.Event.touchUpInside)

    }
    
    @objc func tosearch(){
        
        let diaryList:String = Bundle.main.path(forResource: "zhishi", ofType:"plist")!
        let data = NSDictionary.init(contentsOfFile: diaryList)
        let dataList = data!["list"] as! [String]
        
        let index = arc4random_uniform(UInt32(dataList.count-1))
        
        
        let attr = NSMutableAttributedString.init(string: dataList[Int(index)])
        attr.lineSpacing = 4
        attr.lineBreakMode = .byTruncatingTail
        label.attributedText = attr
    }
    
    @objc func backAct() {
        self.navigationController?.popViewController(animated: true)
    }

}
